﻿using System;
using System.Collections;

namespace WindowsFormsApplication1
{
    class Translate_ave2csp
    {
        public AVEStructure m_fromAVE = null;
        public CSPStructure m_toCSP = null;

        public Translate_ave2csp() { 
            
             
        }

        public CSPStructure translate() {

            if (m_toCSP == null) {
                m_toCSP = new CSPStructure();
            }
            
            //translate the involoved people number
            String condition = "#define " + "N " + m_fromAVE.ps.Count ;
            m_toCSP.defines.Add(condition);
            

            //translate varibles
            foreach (String i in m_fromAVE.globalvaris) {
                String vari = i;
                m_toCSP.vars.Add(translateVariables(vari));
            }

            translateProcess(m_fromAVE.ps);
           

            return m_toCSP;
            
        }

        public String translateVariables(String variables){
            
            //byte pos[4];
            //byte j=0, k=0;
            //var board = [3,5,6,0,2,7,8,4,1];
            //var extrequestsDOWN[NoOfFloors]; 

            String cspvariables = "";

            if (variables.IndexOf("byte") != -1) {

                cspvariables = "var " + variables.Substring(5);

                
            }else if (variables.IndexOf("int") != -1) {

                cspvariables = "var " + variables.Substring(4);
            }

            return cspvariables;


        }

        public void translateProcess(ArrayList ps) {

            Process transp = null;


            
            //after first round, we got all the events
            
            foreach (Process p in ps) {
                
                transp = p;
                
                //different from the others, the variables here are global ones
                foreach (String v in p.localvaris) {

       //             translateVariables(v);
                }

                //start here, we translate the process, actually, changed from process-based to event-based

                Trans t = transp.Trans;

                foreach (String m in t.expressions) {

                  // Left() = [emptypos!=2&&emptypos!=5&&emptypos!=8]goleft{board[emptypos]=board[emptypos+1];board[emptypos+1]=0;emptypos=emptypos+1;} -> Game();
   
                  //  NCS -> wait { effect j = 1; },
                    String eachEvent = m.Substring(0, m.IndexOf("{")-1);

                    String recordEvent = m.Substring(0, m.IndexOf("{")) + "{";

                    foreach (Process pp in ps)
                    {
                        foreach (String mm in t.expressions) {

                            if (mm.IndexOf(eachEvent) != -1) {

                                recordEvent += mm.Substring(mm.IndexOf("{") + 1, mm.IndexOf("}") - mm.IndexOf("{") - 1);
                             //   recordEvent += ";";
                            }
                        }
                    }

                    recordEvent += "}";

                    recordEvent.Replace("->","_To_");

                 
                    m_toCSP.events.Add(recordEvent);
                }

                break;
            }

            /**
            Move1To3()=if((size[1] > 0&&column1[size[1]]<column3[size[3]])||(size[1] >0&&size[3]==0))
	        {
		    move.1.3{size[3]=size[3]+1;column3[size[3]]=column1[size[1]];size[1]=size[1]-1;}->Skip
	        };
            */

           
            
            
        }

    }
}
